#include "rollcadresortfilterproxymodel.h"
#include <QStringList>
#include <QDebug>
#include <QDate>
#include <QString>
#include <QColor>

RollCadreSortFilterProxyModel::RollCadreSortFilterProxyModel(QObject *parent)
  :QSortFilterProxyModel(parent)
{
}

QVariant RollCadreSortFilterProxyModel::data(const QModelIndex &index,
                                             int role) const
{
    if (!index.isValid())
        return QVariant();

    QVariant value=QSortFilterProxyModel::data(index,role);

    if (role == Qt::CheckStateRole)
    {
        if (index.column()==0)
        {
            if (checkStateMap.contains(index.row()))
            {
                return checkStateMap[index.row()] == Qt::Checked ? Qt::Checked : Qt::Unchecked;
            }
            return Qt::Unchecked;
        }
    }

    if(role == Qt::BackgroundColorRole)
        {
            int row =index.row();
            if(!colormap.isEmpty()&& colormap.keys().contains(row))
                return colormap.value(row);
        }
    return value;
}

bool  RollCadreSortFilterProxyModel::setData( const QModelIndex &index,
                                              const QVariant &value, int role )
{
    if(!index.isValid())
        return false;
    if(role == Qt::BackgroundColorRole )
        {
            int row =index.row();
            colormap[row]=value.value<QColor>();
        }
    if (role == Qt::CheckStateRole && index.column() == 0)
    {
        checkStateMap[index.row()] = (value == Qt::Checked ? Qt::Checked : Qt::Unchecked);
    }
    return true;
}

Qt::ItemFlags RollCadreSortFilterProxyModel::flags(const QModelIndex &index) const
{
    if (!index.isValid())
        return 0;

    if (index.column() == 0){
        return Qt::ItemIsEnabled | Qt::ItemIsSelectable | Qt::ItemIsUserCheckable ;
    }
    return  Qt::ItemIsEnabled | Qt::ItemIsSelectable;
}

void RollCadreSortFilterProxyModel::setCadreName(QString cadrename)
{
    fcadrename = cadrename;
    invalidateFilter();

}
void RollCadreSortFilterProxyModel::setPostName(QString postname)
{
    fpostname = postname;
    invalidateFilter();
}
void RollCadreSortFilterProxyModel::setExperPost(QString experpost)
{
    fexperpost = experpost;
    invalidateFilter();
}
void RollCadreSortFilterProxyModel::setExperUnit(QString experunit)
{
    fexperunit = experunit;
    invalidateFilter();
}
void RollCadreSortFilterProxyModel::setUnitName(QString unitname)
{
    funitname = unitname;
    invalidateFilter();
}
void RollCadreSortFilterProxyModel::setSchool(QString school)
{
    fschool = school;
    invalidateFilter();
}

void RollCadreSortFilterProxyModel::setMajor(QString major)
{
    fmajor = major;
    invalidateFilter();
}
void RollCadreSortFilterProxyModel::setWorkSchool(QString workschool)
{
    fworkschool = workschool;
    invalidateFilter();
}
void RollCadreSortFilterProxyModel::setWorkMajor(QString workmajor)
{
    fworkmajor = workmajor;
    invalidateFilter();
}
void RollCadreSortFilterProxyModel::setOrRelation(QString orrelation)
{
    forrelation = orrelation;
    invalidateFilter();
}
void RollCadreSortFilterProxyModel::setFamilyUnit(QString familyunit)
{
    ffamilyunit = familyunit;
    invalidateFilter();
}
void RollCadreSortFilterProxyModel::setFamilyPro(QString familypro)
{
    ffamilypro = familypro;
    invalidateFilter();
}
void RollCadreSortFilterProxyModel::setFileRecv(QString filerecv)
{
    ffilerecv = filerecv;
    invalidateFilter();
}
void RollCadreSortFilterProxyModel::setNativePlace(QStringList native)
{
    fnative = native;
    invalidateFilter();
}
void RollCadreSortFilterProxyModel::setBirthplace(QStringList birthplace)
{
    fbirthplace = birthplace;
    invalidateFilter();
}
void RollCadreSortFilterProxyModel::setGrowPlace(QStringList growplace)
{
    fgrowplace = growplace;
    invalidateFilter();
}

void RollCadreSortFilterProxyModel::setBirth(QStringList birth)
{
    fbirth = birth;
    invalidateFilter();
}
void RollCadreSortFilterProxyModel::setAge(QStringList age)
{
    fage = age;
    invalidateFilter();
}
void RollCadreSortFilterProxyModel::setJoinWorkTime(QStringList joinworktime)
{
    fjoinworktime = joinworktime;
    invalidateFilter();
}

void RollCadreSortFilterProxyModel::setPartyTime(QStringList partytime)
{
    fpartytime = partytime;
    invalidateFilter();
}
void RollCadreSortFilterProxyModel::setRankTime(QStringList ranktime)
{
    franktime = ranktime;
    invalidateFilter();
}
void RollCadreSortFilterProxyModel::setRankYear(QStringList rankyear)
{
    frankyear = rankyear;
    invalidateFilter();
}
void RollCadreSortFilterProxyModel::setPostTime(QStringList posttime)
{
    fposttime = posttime;
    invalidateFilter();
}
void RollCadreSortFilterProxyModel::setPostYear(QStringList postyear)
{
    fpostyear = postyear;
    invalidateFilter();
}
void RollCadreSortFilterProxyModel::setManageStatus(QStringList managestatus)
{
    fmanagestatus = managestatus;
    invalidateFilter();
}
void RollCadreSortFilterProxyModel::setThreeOne(QStringList threeone)
{
    fthreeone = threeone;
    invalidateFilter();
}

void RollCadreSortFilterProxyModel::setSex(QStringList sex)
{
    fsex = sex;
    invalidateFilter();
}
void RollCadreSortFilterProxyModel::setPolitic(QStringList politic)
{
    fpolitic = politic;
    invalidateFilter();
}
void RollCadreSortFilterProxyModel::setExperNull(QStringList expernull)
{
    fexpernull = expernull;
    invalidateFilter();
}
void RollCadreSortFilterProxyModel::setNation(QStringList nationList)
{
    fnationList = nationList;
    invalidateFilter();
}
void RollCadreSortFilterProxyModel::setRank(QStringList rankList)
{
    frankList = rankList;
    invalidateFilter();
}
void RollCadreSortFilterProxyModel::setPostCategoryList(QStringList postcategoryList)
{
    fpostcategoryList = postcategoryList;
    invalidateFilter();
}
void RollCadreSortFilterProxyModel::setFile(QStringList isfileList)
{
    fisfileList = isfileList;
    invalidateFilter();
}
void RollCadreSortFilterProxyModel::setManageStatusList(QStringList managestatusList)
{
    fmanagestatusList = managestatusList;
    invalidateFilter();
}
void RollCadreSortFilterProxyModel::setWorkEducationList(QStringList workeducationList)
{
    fworkeducationList=workeducationList;
    invalidateFilter();
}

void RollCadreSortFilterProxyModel::setWorkDegreeList(QStringList workdegreeList)
{
    fworkdegreeList=workdegreeList;
    invalidateFilter();
}

void RollCadreSortFilterProxyModel::setEducationList(QStringList educationList)
{
    feducationList=educationList;
    invalidateFilter();
}

void RollCadreSortFilterProxyModel::setDegreeList(QStringList degreeList)
{
    fdegreeList=degreeList;
    invalidateFilter();
}

void RollCadreSortFilterProxyModel::setUnitList(QStringList unitList)
{
    funitList=unitList;
    invalidateFilter();
}

void RollCadreSortFilterProxyModel::setUnitGradationList(QStringList unitgradationList)
{
    funitgradationList=unitgradationList;
    invalidateFilter();
}
void RollCadreSortFilterProxyModel::setUnitNatrueList(QStringList unitnatrueList)
{
    funitnatrueList=unitnatrueList;
    invalidateFilter();
}

void RollCadreSortFilterProxyModel::setUnitCategoryList(QStringList unitcategoryList)
{
    funitcategoryList=unitcategoryList;
    invalidateFilter();
}

void RollCadreSortFilterProxyModel::setUnitOfCategoryList(QStringList unitofcategoryList)
{
    funitofcategoryList=unitofcategoryList;
    invalidateFilter();
}

void RollCadreSortFilterProxyModel::setUnitLevelList(QStringList unitlevelList)
{
    funitlevelList=unitlevelList;
    invalidateFilter();
}

void RollCadreSortFilterProxyModel::setSubUnitList(QStringList subunitList)
{
    fsubunitList=subunitList;
    invalidateFilter();
}

void RollCadreSortFilterProxyModel::setCheckYear(QStringList checkyear)
{
    fcheckyear=checkyear;
    invalidateFilter();
}

bool RollCadreSortFilterProxyModel::filterAcceptsRow(int sourceRow,
                                                  const QModelIndex &sourceParent) const
{

    QModelIndex indexSex = sourceModel()->index(sourceRow,columnhash["Sex"],sourceParent);
    QString sex = sourceModel()->data(indexSex).toString();
    if(fsex!=QStringList()&&!(fsex.contains(sex))){
        return false;
    }
    QModelIndex indexPolitc = sourceModel()->index(sourceRow,columnhash["Politic"],sourceParent);
    QString politic = sourceModel()->data(indexPolitc).toString();
    if(fpolitic!=QStringList()&&!(fpolitic.contains(politic))){
        return false;
    }
    QModelIndex indexnation = sourceModel()->index(sourceRow,columnhash["Nation"],sourceParent);
    QString nation = sourceModel()->data(indexnation).toString();
    if(fnationList!=QStringList()&&!(fnationList.contains(nation))){
        return false;
    }
    QModelIndex indexrankname = sourceModel()->index(sourceRow,columnhash["RankName"],sourceParent);
    QString rankname = sourceModel()->data(indexrankname).toString();
    if(frankList!=QStringList()&&!(frankList.contains(rankname))){
        return false;
    }

    QModelIndex indexCadreName = sourceModel()->index(sourceRow,columnhash["CadreName"],sourceParent);
    QString cadrename = sourceModel()->data(indexCadreName).toString();
    if(!fcadrename.isEmpty()&&!(cadrename.contains(fcadrename))){
        return false;
    }
    QModelIndex indexPostName = sourceModel()->index(sourceRow,columnhash["PostName"],sourceParent);
    QString postname = sourceModel()->data(indexPostName).toString();
    if(!fpostname.isEmpty()&&!(postname.contains(fpostname))){
        return false;
    }
    QModelIndex indexUnitName = sourceModel()->index(sourceRow,columnhash["UnitName"],sourceParent);
    QString unitname = sourceModel()->data(indexUnitName).toString();
    if(!funitname.isEmpty()&&!(unitname.contains(funitname))){
        return false;
    }
    // 工作经历过滤experice 中的 postname,unitname
    QModelIndex indexexUnitName = sourceModel()->index(sourceRow,columnhash["experUnitName"],sourceParent);
    QModelIndex indexexPostName = sourceModel()->index(sourceRow,columnhash["experPostName"],sourceParent);
    QString unit = sourceModel()->data(indexexUnitName).toString();
    QString post = sourceModel()->data(indexexPostName).toString();
    if(!fexperunit.isEmpty()&&!(unit.contains(fexperunit))){
        return false;
    }
    if(!fexperpost.isEmpty()&&!(post.contains(fexperpost))){
        return false;
    }

    QModelIndex indexwors = sourceModel()->index(sourceRow,columnhash["WorkORSchool"],sourceParent);
    QModelIndex indexmajor = sourceModel()->index(sourceRow,columnhash["Major"],sourceParent);
    QModelIndex indexschool = sourceModel()->index(sourceRow,columnhash["School"],sourceParent);
    QString workorschool = sourceModel()->data(indexwors).toString();
    QString major = sourceModel()->data(indexmajor).toString();
    QString school = sourceModel()->data(indexschool).toString();
    // 在职学校，专业
    if(!fworkmajor.isEmpty()&& (workorschool!="0"||!(major.contains(fworkmajor)))){
        return false;
    }
    if(!fworkschool.isEmpty()&& (workorschool!="0"||!(school.contains(fworkschool)))){
        return false;
    }
    // 全日制学校，专业
    if(!fmajor.isEmpty()&& (workorschool!="1"||!(major.contains(fmajor)))){
        return false;
    }
    if(!fschool.isEmpty()&& (workorschool!="1"||!(school.contains(fschool)))){
        return false;
    }

    QModelIndex indexFileNum = sourceModel()->index(sourceRow,columnhash["FileNum"],sourceParent);
    QString filenum = sourceModel()->data(indexFileNum).toString();
    if(!ffilerecv.isEmpty()&&!(filenum.contains(ffilerecv))){
        return false;
    }

    QModelIndex indexfprofession = sourceModel()->index(sourceRow,columnhash["fprofession"],sourceParent);
    QModelIndex indexfunitname = sourceModel()->index(sourceRow,columnhash["funitname"],sourceParent);
    QString fpro = sourceModel()->data(indexfprofession).toString();
    QString funame = sourceModel()->data(indexfunitname).toString();
    if(!ffamilyunit.isEmpty()&&!(funame.contains(ffamilyunit))){
        return false;
    }
    if(!ffamilypro.isEmpty()&&!(fpro.contains(ffamilypro))){
        return false;
    }
    QModelIndex indexNativePlace = sourceModel()->index(sourceRow,columnhash["NativePlace"],sourceParent);
    QString nativeplace = sourceModel()->data(indexNativePlace).toString();
    if(!fnative.isEmpty()){
        if(fnative[0]==QString::fromUtf8("属于")&&!(nativeplace.contains(fnative[1])))
            return false;
        if(fnative[0]==QString::fromUtf8("不属于")&& nativeplace.contains(fnative[1]))
            return false;
    }
    QModelIndex indexBirthPlace = sourceModel()->index(sourceRow,columnhash["BirthPlace"],sourceParent);
    QString birthplace = sourceModel()->data(indexBirthPlace).toString();
    if(!fbirthplace.isEmpty()){
        if(fbirthplace[0]==QString::fromUtf8("属于")&&!(birthplace.contains(fbirthplace[1])))
            return false;
        if(fbirthplace[0]==QString::fromUtf8("不属于")&& birthplace.contains(fbirthplace[1]))
            return false;
    }
    QModelIndex indexGrowPlace = sourceModel()->index(sourceRow,columnhash["GrowPlace"],sourceParent);
    QString growplace = sourceModel()->data(indexGrowPlace).toString();
    if(!fgrowplace.isEmpty()){
        if(fgrowplace[0]==QString::fromUtf8("属于")&&!(growplace.contains(fgrowplace[1])))
            return false;
        if(fgrowplace[0]==QString::fromUtf8("不属于")&& growplace.contains(fgrowplace[1]))
            return false;
    }

    QModelIndex indexBirth = sourceModel()->index(sourceRow,columnhash["Birth"],sourceParent);
    QString birth = sourceModel()->data(indexBirth).toString();
    // 必须判断fbirth是否为空,虽然界面上显示肯定不为空，但是此model内部应该有处理，有段结果确实是为空的
    if(!fbirth.isEmpty()&&(birth< fbirth[0] || birth>fbirth[1])){
        return false;
    }
    // 年龄
    int days =sourceModel()->data(indexBirth).toDate().daysTo(QDate::currentDate());
    int nowage = days/365;
    if(!fage.isEmpty()&&(nowage< fage[0].toInt() || nowage>fage[1].toInt())){
        return false;
    }
    QModelIndex indexJoinWorkTime = sourceModel()->index(sourceRow,columnhash["JoinWorkTime"],sourceParent);
    QString joinworktime = sourceModel()->data(indexJoinWorkTime).toString();
    if(!fjoinworktime.isEmpty()&&(joinworktime< fjoinworktime[0] || joinworktime>fjoinworktime[1])){
        return false;
    }
    QModelIndex indexPartyTime = sourceModel()->index(sourceRow,columnhash["PartyTime"],sourceParent);
    QString partytime = sourceModel()->data(indexPartyTime).toString();
    if(!fpartytime.isEmpty()&&(partytime< fpartytime[0] || partytime>fpartytime[1])){
        return false;
    }
    //任职时间，任职年限 涉及单表项目间的对比,在sql中提现
    QModelIndex indexRankTime = sourceModel()->index(sourceRow,columnhash["RankTime"],sourceParent);
    QString ranktime = sourceModel()->data(indexRankTime).toString();
    if(!franktime.isEmpty()&&(ranktime< franktime[0] || ranktime>franktime[1])){
        return false;
    }

    int rdays =sourceModel()->data(indexRankTime).toDate().daysTo(QDate::currentDate());
    int nowyears = rdays/365;
    if(!frankyear.isEmpty()&&(nowyears< frankyear[0].toInt() || nowyears>frankyear[1].toInt())){
        return false;
    }

    QModelIndex indexManageStatus = sourceModel()->index(sourceRow,columnhash["ManageStatus"],sourceParent);
    QString managestatus = sourceModel()->data(indexManageStatus).toString();
    int emptyflag =0;
    if(!fmanagestatus.isEmpty()){
        foreach(QString v, fmanagestatus){
            if(v.isEmpty()){
                emptyflag++;
            }
        }
        if(emptyflag!=3 &&(managestatus.isNull()||!fmanagestatus.contains(managestatus)))
        {
            if(fmanagestatus.contains(QString::fromUtf8("其他"))
                    && managestatus!=QString::fromUtf8("离退")
                    && managestatus!=QString::fromUtf8("在职"))
            {
                return true;
            }
            return false;
        }
    }



    QModelIndex indexThreeOne = sourceModel()->index(sourceRow,columnhash["ThreeOne"],sourceParent);
    QString threeone = sourceModel()->data(indexThreeOne).toString();
    int emptyflag1 =0;
    if(!fthreeone.isEmpty()){
        foreach(QString v, fthreeone){
            if(v.isEmpty()){
                emptyflag1++;
            }
        }
        if(emptyflag1!=2 &&(threeone.isNull()|| !fthreeone.contains(threeone))){
            return false;
        }
    }

    // 工作经历为空，经历表里职务或者单位为空
    if(!fexpernull.isEmpty()&&fexpernull[0]=="1"&& !(unit.isEmpty())&&!(unit.isNull())
            &&!(post.isEmpty())&& !(post.isNull())){
        return false;
    }
    // 年度考核连续性
    QModelIndex indexmin = sourceModel()->index(sourceRow,columnhash["mincheckyear"],sourceParent);
    QModelIndex indexmax = sourceModel()->index(sourceRow,columnhash["maxcheckyear"],sourceParent);
    QModelIndex indexcount = sourceModel()->index(sourceRow,columnhash["countcheckyear"],sourceParent);
    int min = sourceModel()->data(indexmin).toInt();
    int max = sourceModel()->data(indexmax).toInt();
    int ccount = sourceModel()->data(indexcount).toInt();
    if(!fcheckyear.isEmpty()&&fcheckyear[0]=="1")
    {
        if(ccount==(max-min+1)){
            return false;
        }
     }
    // 树形条件
    if(!fisfileList.isEmpty()){
        if(!filenum.isEmpty()&&fisfileList.contains(QString::fromUtf8("否"))){
            return false;
        }
        if(filenum.isEmpty()&&fisfileList.contains(QString::fromUtf8("是"))){
            return false;
        }
    }
    if(!fmanagestatusList.isEmpty()&&!(fmanagestatusList.contains(managestatus))){
        return false;
    }
    if(!fmanagestatusList.isEmpty()&&!(fmanagestatusList.contains(managestatus))){
        return false;
    }
    QModelIndex indexPostCategory = sourceModel()->index(sourceRow,columnhash["PostCategory"],sourceParent);
    QString postcategory = sourceModel()->data(indexPostCategory).toString();
    if(!fpostcategoryList.isEmpty()&&!(fpostcategoryList.contains(postcategory))){
        return false;
    }
    QModelIndex indexEducation = sourceModel()->index(sourceRow,columnhash["Education"],sourceParent);
    QString education = sourceModel()->data(indexEducation).toString();

    QModelIndex indexDegree= sourceModel()->index(sourceRow,columnhash["Degree"],sourceParent);
    QString degree = sourceModel()->data(indexDegree).toString();
    if(!fdegreeList.isEmpty()&&!(fdegreeList.contains(degree))){
        return false;
    }
    // 在职学历与全日制学历为或关系
    if(forrelation=="1"){
        if(!feducationList.isEmpty()&&!(feducationList.contains(education))
           &&!fworkeducationList.isEmpty()&&!(fworkeducationList.contains(education)))
        {
            return false;
        }
    }else{
        if(!feducationList.isEmpty()&&!(feducationList.contains(education))){
            return false;
        }
        if(!fworkeducationList.isEmpty()&&!(fworkeducationList.contains(education))){
            return false;
        }
    }

    if(!fworkdegreeList.isEmpty()&&!(fworkdegreeList.contains(degree))){
        return false;
    }
    if(!funitList.isEmpty()&&!(funitList.contains(unitname))){
        return false;
    }
    QModelIndex indexUnitGradationName= sourceModel()->index(sourceRow,columnhash["UnitGradationName"],sourceParent);
    QString unitgradationname = sourceModel()->data(indexUnitGradationName).toString();
    if(!funitgradationList.isEmpty()&&!(funitgradationList.contains(unitgradationname))){
        return false;
    }
    QModelIndex indexUnitNatrueName= sourceModel()->index(sourceRow,columnhash["UnitNatrueName"],sourceParent);
    QString unitnatruename = sourceModel()->data(indexUnitNatrueName).toString();
    if(!funitnatrueList.isEmpty()&&!(funitnatrueList.contains(unitnatruename))){
        return false;
    }
    QModelIndex indexUnitCategory= sourceModel()->index(sourceRow,columnhash["UnitCategory"],sourceParent);
    QString unitcategoryList = sourceModel()->data(indexUnitCategory).toString();
    if(!funitcategoryList.isEmpty()&&!(funitcategoryList.contains(unitcategoryList))){
        return false;
    }
    QModelIndex indexUnitOfCategoryName= sourceModel()->index(sourceRow,columnhash["UnitOfCategoryName"],sourceParent);
    QString cunitofcategoryname = sourceModel()->data(indexUnitOfCategoryName).toString();
    if(!funitofcategoryList.isEmpty()&&!(funitofcategoryList.contains(cunitofcategoryname))){
        return false;
    }
    QModelIndex indexUnitLevel= sourceModel()->index(sourceRow,columnhash["UnitLevel"],sourceParent);
    QString cunitlevel = sourceModel()->data(indexUnitLevel).toString();
    if(!funitlevelList.isEmpty()&&!(funitlevelList.contains(cunitlevel))){
        return false;
    }
    QModelIndex indexSubUnit= sourceModel()->index(sourceRow,columnhash["AffiliationName"],sourceParent);
    QString csubunit = sourceModel()->data(indexSubUnit).toString();
    if(!fsubunitList.isEmpty()&&!(fsubunitList.contains(csubunit))){
        return false;
    }
        return true;
}
